﻿/*
 * modbusrtmaster.hpp
 *
 *  Created on: 2017年3月14日
 *      Author: work
 */

#ifndef _DM_PROTOCOL_MODBUSRTMASTER_HPP_
#define _DM_PROTOCOL_MODBUSRTMASTER_HPP_

#include <dm/export.hpp>

#ifndef DM_API_PROTOCOL
#define DM_API_PROTOCOL DM_API_IMPORT
#endif

#include <dm/protocol/modbusrtu.hpp>

namespace dm{
namespace protocol{

/**
 * Modbus主站类
 * 定时主动发送数据，并等待反馈
 */
class DM_API_PROTOCOL CModbusRtuMaster:virtual public CModbusRtu{
public:
	CModbusRtuMaster();
	virtual ~CModbusRtuMaster();

	EAction dealRxFrame( const ts_t& ts,const rts_t& rts );

protected:
	CFrameModbusRtu* genRxTempFrame();
	CFrameModbusRtu* genTxTempFrame();

	/**
	 * 启动召唤任务
	 * 如果当前没有等待应答，则调用modbus的启动函数
	 * @param txFrame
	 * @return
	 */
	EAction taskStart_call( const ts_t& ts,const rts_t& rts );

	/**
	 * 执行召唤任务
	 * 如果当前没有等待应答，则调用modbus相关函数
	 * @param txFrame
	 * @return
	 */
	EAction taskDo_call( const ts_t& ts,const rts_t& rts );

	/**
	 * 启动时钟同步任务
	 * @param txFrame
	 * @return
	 */
	EAction taskStart_syncClock( const ts_t& ts,const rts_t& rts );

	/**
	 * 执行时钟同步任务
	 * @param txFrame
	 * @return
	 */
	EAction taskDo_syncClock( const ts_t& ts,const rts_t& rts );

	EAction taskStart_remoteControl( const ts_t& ts,const rts_t& rts );
	EAction taskDo_remoteControl( const ts_t& ts,const rts_t& rts );

	EAction taskStart_parameters( const ts_t& ts,const rts_t& rts );
	EAction taskDo_parameters( const ts_t& ts,const rts_t& rts );

	//=========
	/**
	 * 启动召唤任务
	 * @param tf
	 * @return
	 */
	virtual EAction taskStart_call( CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction taskDo_call( CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction taskStart_syncClock( CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction taskDo_syncClock( CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction taskStart_remoteControl( CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction taskDo_remoteControl( CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction taskStart_parameters( CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction taskDo_parameters( CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction dealRxResponse_readCoils( const CFrameModbusRtu& rf,CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction dealRxResponse_readDiscreteInputs( const CFrameModbusRtu& rf,CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction dealRxResponse_readHoldingRegisters( const CFrameModbusRtu& rf,CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts);
	virtual EAction dealRxResponse_readInputRegisters( const CFrameModbusRtu& rf,CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction dealRxResponse_writeSingleCoil( const CFrameModbusRtu& rf,CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction dealRxResponse_writeSingleRegister( const CFrameModbusRtu& rf,CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );

	virtual EAction dealRxResponse_writeMultipleCoils( const CFrameModbusRtu& rf,CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );
	virtual EAction dealRxResponse_writeMultipleRegisters( const CFrameModbusRtu& rf,CFrameModbusRtu& tf,const ts_t& ts,const rts_t& rts );

	virtual void coils_01_error( const dm::uint16& address,const CFrameModbusRtu::EErrCode& ec,const ts_t& ts,const rts_t& rts );
	virtual void coils_01( const dm::uint16& address,const dm::uint8& value,const ts_t& ts,const rts_t& rts );

	virtual void discreteInputs_02_error( const dm::uint16& address,const CFrameModbusRtu::EErrCode& ec,const ts_t& ts,const rts_t& rts );
	virtual void discreteInputs_02( const dm::uint16& address,const dm::uint8& value,const ts_t& ts,const rts_t& rts );

	virtual void holdingRegisters_03_error( const dm::uint16& address,const CFrameModbusRtu::EErrCode& ec,const ts_t& ts,const rts_t& rts );
	virtual void holdingRegisters_03( const dm::uint16& address,const dm::uint16& value,const ts_t& ts,const rts_t& rts );

	virtual void inputRegisters_04_error( const dm::uint16& address,const CFrameModbusRtu::EErrCode& ec,const ts_t& ts,const rts_t& rts );
	virtual void inputRegisters_04( const dm::uint16& address,const dm::uint16& value,const ts_t& ts,const rts_t& rts );
};

}
}

#endif /* INCLUDE_DM_PROTOCOL_MODBUSRTMASTER_HPP_ */
